import waves from "@/directive/waves";
import { mapGetters } from 'vuex';
import elDragDialog from "@/directive/el-drag-dialog";
import {
  getMenuTree,
  getMenuPermissions,
  deletePermission,
  deleteMenu,
  editMenu,
  createPermission, createMenu
} from "./module-management.service";
import { alertMessage, alertWithCallback } from "@/utils/utils";

export default {
  name: "module-management",
  components: { },
  directives: { waves, elDragDialog },
  data() {
    return {
      departmentEditRules: {
        name: [{ required: true, trigger: "change", message: '此为必填项' }],
        code: [{ required: true, trigger: "change", message: '此为必填项' }],
        path: [{ required: true, trigger: "change", message: '此为必填项' }],
        sortCode: [{ required: true, trigger: "change", message: '此为必填项' }],
        pid: [{ required: true, trigger: "change", message: '此为必填项' }],
        permission: [{ required: true, trigger: "change", message: '此为必填项' }],
        serviceCode: [{ required: true, trigger: "change", message: '此为必填项' }]
      },
      dialogTitle: "",
      keyCodeList: [],
      pid: 0,
      rowInfo: {},
      departmentEditForm: {},
      departmentCreateForm: {},
      keyCodeData: {},
      editDialogFormVisible: false, // 编辑弹窗
      createDialogFormVisible: false, // 创建弹窗
      createKeyCodeFormVisible: false, // 创建操作码
      formData: {
        data: [],
        expandAll: true
      }
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getSysStructsTree();
  },
  methods: {
    // 获取模块树
    getSysStructsTree() {
      getMenuTree().then(res => {
        this.formData.data = res.data;
        this.keyCodeList = [];
      });
    },

    // 校验非空  创建
    createStruct() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.confirmCreat();
        }
      });
    },

    confirmCreat() {
      alertWithCallback('模块别名创建后就无法修改，您确定创建吗?', '提示').then(() => {
        this.createModule();
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 校验非空 编辑
    editStruct() {
      this.$refs.dataFormEdit.validate(valid => {
        if (valid) {
          this.editModule();
        }
      });
    },

    createKeyCode() {
      this.$refs.dataFormKeyCode.validate(valid => {
        if (valid) {
          this.confirmCreateKeyCode();
        }
      });
    },

    // 创建操作码
    confirmCreateKeyCode() {
      createPermission(this.keyCodeData, this.rowInfo.code).then((res) => {
        alertMessage(res.message);
        this.getList(this.rowInfo);
        this.createKeyCodeFormVisible = false;
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 创建
    createModule() {
      createMenu(this.departmentCreateForm).then((res) => {
        alertMessage(res.message);
        this.getSysStructsTree();
        this.createDialogFormVisible = false;
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 编辑
    editModule() {
      editMenu(this.departmentEditForm).then((res) => {
        alertMessage(res.message);
        this.editDialogFormVisible = false;
        this.getSysStructsTree();
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 删除模块树
    delSysStruct(row) {
      deleteMenu([row.id]).then((res) => {
        alertMessage(res.message);
        this.getSysStructsTree();
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 确认删除
    confirmDelete(row) {
      alertWithCallback('您确定要删除该模块吗?', '提示').then(() => {
        this.delSysStruct(row);
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 确认删除操作码
    confirmDeleteKeyCode(row) {
      alertWithCallback('您确定要删除该操作码吗?', '提示').then(() => {
        this.delKeyCodeStruct(row);
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 删除操作码
    delKeyCodeStruct(row) {
      deletePermission([row.objectId]).then((res) => {
        alertMessage(res.message);
        this.getList(this.rowInfo);
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 获取操作码
    getList(row) {
      this.rowInfo = row;
      getMenuPermissions(row.code).then((res) => {
        this.keyCodeList = res.data;
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 打开弹窗
    openDialogForm(dialog, row) {
      this.departmentEditForm = {};
      if (dialog === "editDialogFormVisible") {
        this.editDialogFormVisible = true;
        row.sortCode = row.sort;
        this.departmentEditForm = Object.assign({}, row);
        this.dialogTitle = "编辑模块";
      } else if (dialog === "createDialogFormVisible") {
        this.createDialogFormVisible = true;
        this.departmentCreateForm = {};
        this.departmentCreateForm.pid = row ? row.id : 0;
        this.dialogTitle = "创建模块";
      } else if (dialog === 'createKeyCodeFormVisible') {
        this.createKeyCodeFormVisible = true;
        this.keyCodeData = {};
        this.dialogTitle = "创建操作码";
      }
    }
  }
}
